package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;

public class TermActivateStatisticsSearchCondition extends BaseSearchCondition  {	
	
	private String  agentId;// 一级代理商
	private String  agentName;// 一级代理商名称
	
	private String  serialNum ;// SN
	private String  posBrand ;// 厂商
	private String  startBrandDate ;// 绑定开始时间(年月日)
	private String  endBrandDate ;// 绑定结束时间(年月日)
	private String  startTrnDate ;// 激活开始时间(年月日)
	private String  endTrnDate ;// 激活结束时间(年月日)
	
	private String obj;
	private String orgCode;// 机构号
	private String statisticsType; //统计类型

	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();
	/*
		if (Constance.isNotEmpty(agentId))
		{
			filedBeanList.add(new WhereHQLFieldBean("TT.AGENT_ID", agentId.trim(), "=", false));
		}*/
		if (Constance.isNotEmpty(agentName))
		{
			filedBeanList.add(new WhereHQLFieldBean("b.oneagentname", agentName.trim(), "=", false));
		}
		// SN 	
		if (Constance.isNotEmpty(serialNum))
		{
			filedBeanList.add(new WhereHQLFieldBean("b.serial_num", serialNum.trim(), "=", false));
		}
		// 厂商
		if (Constance.isNotEmpty(posBrand) && !posBrand.equals("-1")) {
			filedBeanList.add(new WhereHQLFieldBean("b.pos_brand", posBrand.trim(), "=", false));
		}	
		// 绑定日期 
		if (Constance.isNotEmpty(startBrandDate) && Constance.isNotEmpty(endBrandDate)) {
			String startDate = startBrandDate.replace("-", "");
			String endDate = endBrandDate.replace("-", "");
			
			filedBeanList.add(new WhereHQLFieldBean(
					"to_char(b.brand_date,'yyyyMMdd')", startDate, ">=",
					false));
			filedBeanList
					.add(new WhereHQLFieldBean(
							"to_char(b.brand_date,'yyyyMMdd')", endDate,
							"<=", false));
		}
		
		// 激活日期
		if (Constance.isNotEmpty(startTrnDate) && Constance.isNotEmpty(endTrnDate)) {
			String startDate = startTrnDate.replace("-", "");
			String endDate = endTrnDate.replace("-", "");
		//	startDate = startDate.length() == 7 ?startDate + "0" : startDate;
		//	endDate = endDate.length() == 7 ?endDate + "0" : endDate;
			
			filedBeanList.add(new WhereHQLFieldBean(
					"to_char(b.trn_date)", startDate, ">=",
					false));
			filedBeanList
					.add(new WhereHQLFieldBean(
							"to_char(b.trn_date)", endDate,
							"<=", false));
		}
		return filedBeanList;
	}

	@Override
	protected String getRowCountHQL() {
		StringBuffer sb=new StringBuffer();
			sb.append(" select count(1) ");
			sb.append("from (select tmp.real_name,");
			sb.append("(select o1.real_name from t_operators o1 where to_char(o1.id) = tmp.companyid ) companyname,");
			sb.append("(select o2.real_name from t_operators o2 where to_char(o2.id) = tmp.oneagentid) oneagentname,");
			sb.append("a.serial_num,a.pos_brand,a.brand_date,a.trn_date,a.term_id,a.mchnt_cd,a.dt_create,a.dt_lastmod ");
			sb.append("from t_term_activate_statistics a,");
			sb.append("(select to_char(s.id) tid,s.real_name,s.companyid,s.oneagentid from t_operators s start with id = " +obj);
			sb.append(" connect by prior s.id = s.supri_id and s.role_type = '03') tmp where ");
			 if("3".equals(statisticsType)){
					sb.append(" a.status = '2' ");
				}else{
					sb.append(" a.status = '1' ");
				}
			sb.append("and tmp.tid = a.agent_id ) b where 1=1  ");
			/*System.out.println(sb.toString());*/
		return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
		StringBuffer sb=new StringBuffer();
		sb.append(" select * from ( ");
			sb.append(" select tmp.real_name,");
			sb.append("(select o1.real_name from t_operators o1 where to_char(o1.id) = tmp.companyid ) companyname,");
			sb.append("(select o2.real_name from t_operators o2 where to_char(o2.id) = tmp.oneagentid) oneagentname,");
			sb.append("a.serial_num,a.pos_brand,a.brand_date,a.trn_date,a.term_id,a.mchnt_cd,to_char(a.dt_create,'yyyy-MM-dd HH24:mi:ss') dt_create, to_char(a.dt_lastmod,'yyyy-MM-dd HH24:mi:ss') dt_lastmod  ,tmp.oneagentid ");
			sb.append("from t_term_activate_statistics a,");
			sb.append("(select to_char(s.id) tid,s.real_name,s.companyid,s.oneagentid from t_operators s start with id = " +obj);
			sb.append(" connect by prior s.id = s.supri_id and s.role_type = '03') tmp where ");
			 if("3".equals(statisticsType)){
					sb.append(" a.status = '2' ");
				}else{
					sb.append(" a.status = '1' ");
				}
			sb.append("and tmp.tid = a.agent_id ) b where 1=1  ");
			/*System.out.println(sb.toString());*/
		return sb.toString();
	}
	
	@Override
	protected String getOrderPartOfHQL() {
		return " order by b.trn_date desc ";
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getAgentName() {
		return agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	public String getPosBrand() {
		return posBrand;
	}

	public void setPosBrand(String posBrand) {
		this.posBrand = posBrand;
	}

	public String getStartBrandDate() {
		return startBrandDate;
	}

	public void setStartBrandDate(String startBrandDate) {
		this.startBrandDate = startBrandDate;
	}

	public String getEndBrandDate() {
		return endBrandDate;
	}

	public void setEndBrandDate(String endBrandDate) {
		this.endBrandDate = endBrandDate;
	}

	public String getStartTrnDate() {
		return startTrnDate;
	}

	public void setStartTrnDate(String startTrnDate) {
		this.startTrnDate = startTrnDate;
	}

	public String getEndTrnDate() {
		return endTrnDate;
	}

	public void setEndTrnDate(String endTrnDate) {
		this.endTrnDate = endTrnDate;
	}
/*
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	*/

	public String getStatisticsType() {
		return statisticsType;
	}

	public void setStatisticsType(String statisticsType) {
		this.statisticsType = statisticsType;
	}
	
}
